//
//  MAVipMealView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/15.
//

#import "MAVipMealView.h"

@interface MAVipMealView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *cornerView;
@property (nonatomic, strong) UILabel *tagView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *originLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSNumber *origin;
@property (nonatomic, strong) NSNumber *price;
@property (nonatomic, strong) NSNumber *isHot;

@end

@implementation MAVipMealView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface {
    _contentView = [UIView new];
    _contentView.layer.cornerRadius = MARatioWidth(16);
    _contentView.clipsToBounds = YES;
    _contentView.backgroundColor = UIColorWhite;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    
    
    _titleLabel = [UILabel ma_labelWithSuperView:_contentView text:@"标题" textFont:MAFontScaleSemibold(18) textColor:[UIColor flatBlackColor] textAlignment:NSTextAlignmentCenter makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.centerX.mas_equalTo(0);
        make.top.ma_offset(26);
    }];
    
    _originLabel = [UILabel ma_labelWithSuperView:_contentView text:@"" textFont:MAFontScaleSemibold(14) textColor:MAColor(@"#666666") textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(16);
        make.top.ma_offset(59);
    }];
    NSString *oldPrice = @"￥ 20";
    NSUInteger length = [oldPrice length];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:MAColor(@"#666666") range:NSMakeRange(0, length)];
    _originLabel.attributedText = attri;
    
    NSString *name = @"PingFangSC-Semibold";
    for (NSString *familyName in [UIFont familyNames]) {// 注意，familyNames 获取到的字体大全里不包含系统默认字体（iOS 13 是 .SFUI，iOS 12 及以前是 .SFUIText）
        for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
            if ([fontName containsString:@"AvenirNext-Heavy"]) {
                name = fontName;
                break;
            }
        }
    }
    _priceLabel = [UILabel ma_labelWithSuperView:_contentView text:@"¥ 28.00" textFont:MAFontName(name, 18, YES) textColor:MAColor(@"#FF0071") textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(16);
        make.top.ma_offset(93);
    }];
    
    _cornerView = [UIButton buttonWithType:UIButtonTypeCustom];
    _cornerView.layer.cornerRadius = MARatioWidth(17);
    _cornerView.clipsToBounds = YES;
    _cornerView.layer.borderColor = MAColor(@"#530027").CGColor;
    _cornerView.layer.borderWidth = MARatioWidth(4);
    [_cornerView addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cornerView];
    [_cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.ma_offset(-2);
        make.right.bottom.ma_offset(2);
    }];
    
    _tagView = [UILabel ma_labelWithSuperView:self text:@"高手必备" textFont:MAFontScaleMedium(10) textColor:UIColorWhite textAlignment:NSTextAlignmentCenter makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.height.ma_offset(27);
        make.width.ma_offset(63);
        make.centerX.mas_equalTo(0);
        make.top.ma_offset(-11);
    }];
    _tagView.backgroundColor = MAColor(@"#530027");
    [_tagView ma_addRoundedCorners:UIRectCornerTopLeft | UIRectCornerBottomRight withRadiif:MARatioWidth(8)];
}

- (void)buttonEvent {
    MABlock(self.selectBlock, self.tag);
}

- (void)setModel:(MAMealItemModel *)model {
    _model = model;
    self.title = model.title;
    self.origin = model.orgPrice;
    self.price = model.price;
    self.isHot = model.isHot;
}

- (void)setTitle:(NSString *)title {
    _titleLabel.text = _title = title;
}

- (void)setOrigin:(NSNumber *)origin {
    _origin = origin;
    NSString *priceStr = [NSString stringWithFormat:@"￥ %@", origin];
    NSUInteger length = [priceStr length];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:MAColor(@"#666666") range:NSMakeRange(0, length)];
    _originLabel.attributedText = attri;
}

- (void)setPrice:(NSNumber *)price {
    _price = price;
    NSString *priceStr = [NSString stringWithFormat:@"￥ %@", price];
    _priceLabel.text = priceStr;
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    if (selected){
        _cornerView.layer.borderWidth = MARatioWidth(4);
    } else {
        _cornerView.layer.borderWidth = 0;
    }
}

- (void)setIsHot:(NSNumber *)isHot {
    _isHot = isHot;
    if (isHot.integerValue == 1) {
        _tagView.hidden = NO;
    } else {
        _tagView.hidden = YES;
    }
}
@end
